package com.mybatis.panel;

import com.mybatis.entity.AwardPunish;
import com.mybatis.entity.Sstatus;
import com.mybatis.entity.Student;
import com.mybatis.service.AwardPunishService;
import com.mybatis.service.SstatusService;
import com.mybatis.service.StudentService;
import com.mybatis.util.DataUtil;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

public class OtherPanel extends JPanel {

    public static int pageType = 0;

    public OtherPanel(int page) {
        pageType = page;
        setBounds(0, 0, 900, 600);

        JPanel panel2 = new JPanel();
        add(panel2, BorderLayout.SOUTH);

        //===========================================
        //JPanel panel = new JPanel(new BorderLayout());
        //panel.setBounds(0, 0, 900, 600);//位置
        //panel.add(new JLabel("用户名"));//文字

        JButton button = new JButton("返回");
        add(button);

        Student student = StudentService.getBySno(StudentPanel.dataRow);

        JLabel labelSname = new JLabel();
        labelSname.setText("当前查看的学生姓名: " + student.getSname());
        add(labelSname);

        JLabel labelSno = new JLabel();
        labelSno.setText("  学号: " + student.getSno());
        add(labelSno);

        JButton awardButton = new JButton("查看奖惩情况");
        JButton statusButton = new JButton("查看学籍信息");
/*        JButton scButton = new JButton("查看课程成绩");
        JButton courseButton = new JButton("查看课程表");*/
        add(awardButton);
        add(statusButton);
        /*add(scButton);
        add(courseButton);*/


        //================================================

        //表格
        JTable table = new JTable() {
            @Override
            public boolean isCellEditable(int row, int column) {
                //可以直接编辑表格
                return true;
            }
        };

        Object[] columnNames = {"学号", "姓名", "性别", "民族", "年龄", "出生日期", "班级编号", "专业编号", "学院编号", "", "", ""};

        List list = new ArrayList<>();

        list.add(student);
        Object[][] objects = DataUtil.getConvertData(list);
        DefaultTableModel tableModel = new DefaultTableModel(objects, columnNames);
        //设置表格数据只能单选
        table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        //设置表格数据
        table.setModel(tableModel);
        //panel.add(table);
        //panel.add(table.getTableHeader(), BorderLayout.NORTH);
        add(table.getTableHeader(), BorderLayout.NORTH);
        //把表格内容添加到容器中心
        //panel.add(table, BorderLayout.CENTER);
        add(table, BorderLayout.CENTER);


        JButton insertButton = new JButton("添加");
        JButton updateButton = new JButton("修改");
        JButton deleteButton = new JButton("删除");
        add(insertButton, BorderLayout.SOUTH);
        add(updateButton, BorderLayout.SOUTH);
        add(deleteButton, BorderLayout.SOUTH);

        //==========================按钮点击触发的事件============================

        button.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                MainJFrame.setPanel(new StudentPanel());
            }
        });

        awardButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                String sno = student.getSno();
                AwardPunish awardPunish = AwardPunishService.getBySno(sno);
                List<AwardPunish> list = new ArrayList<>();
                list.add(awardPunish);
                Object[] columnNames = {"奖惩记录号", "学号", "奖惩记录详情", "", "", "", "", "", "", "", ""};
                Object[][] objects = DataUtil.getConvertData(list);
                DefaultTableModel tableModel = new DefaultTableModel(objects, columnNames);
                table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
                table.setModel(tableModel);
                pageType = 1;
            }
        });

        statusButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                String sno = student.getSno();
                Sstatus sstatus = SstatusService.getById(sno);
                List<Sstatus> list = new ArrayList<>();
                list.add(sstatus);
                Object[] columnNames = {"学号", "是否有学籍", "入学时间", "毕业时间", "学生类别", "学制", "", "", "", "", ""};
                Object[][] objects = DataUtil.getConvertData(list);
                DefaultTableModel tableModel = new DefaultTableModel(objects, columnNames);
                table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
                table.setModel(tableModel);
                pageType = 2;
                //table.is
            }
        });


        /*
        课程成绩表的相关操作 自行完成
        * */
        /*scButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {

            }
        });*/

        /*
        课程表的相关操作 自行完成
        * */
        /*courseButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {

            }
        });*/

        insertButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                Object value = table.getValueAt(0, 0);
                if (pageType == 2) {
                    System.out.println(value);
                    if (value != null) {
                        JOptionPane.showMessageDialog(panel2,
                                "该学生学籍信息不能新增,请进行修改操作", "系统提示", JOptionPane.WARNING_MESSAGE);
                        return;
                    }
                    MainJFrame.setPanel(new SstatusInsertPanel());
                }
                if (pageType == 1) {
                    System.out.println(value);
                    if (value != null) {
                        JOptionPane.showMessageDialog(panel2,
                                "该学生奖惩信息不能新增,请进行修改操作", "系统提示", JOptionPane.WARNING_MESSAGE);
                        return;
                    }
                    MainJFrame.setPanel(new AwardPunishInsertPanel());
                }
            }
        });

        updateButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                Object value = table.getValueAt(0, 0);
                if (pageType == 1) {
                    if (value == null) {
                        JOptionPane.showMessageDialog(panel2,
                                "目前表中没有数据,请先新增数据", "系统提示", JOptionPane.WARNING_MESSAGE);
                        return;
                    }
                    AwardPunish awardPunish = new AwardPunish();
                    Object[] objects = new Object[9];
                    for (int i = 0; i < 3; i++) {
                        objects[i] = table.getValueAt(0, i);
                        //System.out.println("======================================");
                        //System.out.println(table.getValueAt(0, i).getClass());
                    }
                    awardPunish.setApno((String) objects[0]);
                    awardPunish.setSno((String) objects[1]);
                    awardPunish.setAprecord((String) objects[2]);
                    AwardPunishService.update(awardPunish);
                    JOptionPane.showMessageDialog(panel2,
                            "修改成功!", "系统提示", JOptionPane.WARNING_MESSAGE);
                    MainJFrame.setPanel(new OtherPanel(0));
                }
                if (pageType == 2) {
                    if (value == null) {
                        JOptionPane.showMessageDialog(panel2,
                                "目前表中没有数据,请先新增数据", "系统提示", JOptionPane.WARNING_MESSAGE);
                        return;
                    }
                    Sstatus sstatus = new Sstatus();
                    Object[] objects = new Object[9];
                    for (int i = 0; i < 6; i++) {
                        objects[i] = table.getValueAt(0, i);
                        //System.out.println("======================================");
                        //System.out.println(table.getValueAt(0, i).getClass());
                    }
                    sstatus.setSno((String) objects[0]);
                    sstatus.setSstatu((String) objects[1]);
                    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
                    sstatus.setEtime(LocalDate.parse(objects[2].toString(), formatter));
                    sstatus.setGtime(LocalDate.parse(objects[3].toString(), formatter));
                    sstatus.setStype((String) objects[4]);
                    sstatus.setXuezhi(Integer.parseInt( objects[5].toString()));

                    SstatusService.update(sstatus);
                    JOptionPane.showMessageDialog(panel2,
                            "修改成功!", "系统提示", JOptionPane.WARNING_MESSAGE);
                    MainJFrame.setPanel(new OtherPanel(0));
                }
            }
        });

        deleteButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                Object value = table.getValueAt(0, 0);
                int row = table.getSelectedRow();
                if (value == null) {
                    JOptionPane.showMessageDialog(panel2,
                            "目前表中没有数据,请先新增数据", "系统提示", JOptionPane.WARNING_MESSAGE);
                    return;
                } else if (row == -1){
                    JOptionPane.showMessageDialog(panel2,
                            "请选择一行数据", "系统提示", JOptionPane.WARNING_MESSAGE);
                    return;
                }
                if (pageType==0) return;
                if (pageType == 1)
                    AwardPunishService.delete(student.getSno());
                if (pageType == 2)
                    SstatusService.delete(student.getSno());
                JOptionPane.showMessageDialog(panel2,
                            "删除成功", "系统提示", JOptionPane.WARNING_MESSAGE);
                MainJFrame.setPanel(new OtherPanel(0));
            }
        });

    }
}
